<template>
  <div>
    <el-upload class="avatar-uploader" action :show-file-list="true" :before-upload="beforeAvatarUpload"
      list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script>

import ObsClient from "esdk-obs-browserjs/src/obs";
export default {
  data() {
    return {
      imageUrl: "",
      initiateData: {
        option: {},
        uploadId: "",
      },
      fileUrl: "",
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },

  methods: {


    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },


    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      // return isJPG && isLt2M;
      if (isJPG && isLt2M) {
        var obsClient = new ObsClient({
          access_key_id: "CEQDVNAZTLD4SDTQFCTA", // 你的ak
          secret_access_key: "hr4oEqbvC7R0nW6BgqQKI6fA9a6FznVr4W7nGqLM", // 你的sk
          server: "https://obs.cn-north-4.myhuaweicloud.com", // 你的endPoint  记得加入https://
        });
        obsClient.putObject(
          {
            Bucket: "daoxiangcun", // 桶名
            Key: file.name, // 文件名    
            SourceFile: file, //流文件
          });
        this.dialogImageUrl = `https://daoxiangcun.obs.cn-north-4.myhuaweicloud.com/${file.name}`
      }

    },


  },
  updated() {
    console.log(this.dialogImageUrl);
  }
};
</script>